<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>添加/修改游戏</title>
<%@ include file="/view/common/tags.jsp"%>
</head>
<body>
	<div class="well well_p" >
		<sk:form method="post" cssClass="form-horizontal" action="" id="saveAndUpdateFrom" target="_parent"  enctype="multipart/form-data">
			<div class="form-group">
				<label class="col-xs-2 text-right">游戏运营商:</label>
				<div class="col-xs-4">
                    <sk:select cssClass="form-control select" id="operatorId" name="operatorId" value="" requiredInit="true" ajaxURL="${userInfoV1}/baseOperator/operatorSelect?wm[_NOT_EQUAL$ROLE_TYPE]=1"  >
                         <option value="">请选择</option>
                    </sk:select>
					<input type="hidden" name="operatorName" id="operatorName" value="">
				</div>
			</div>
			<div class="form-group">
				<label class="col-xs-2 text-right">游戏名称:</label>
				<div class="col-xs-4">
					<input type="text" class="input" name="gameName" id="gameName" value=""  placeholder="请输入游戏名称" onblur="checkGameName();">
				</div>
			</div>
			<div class="form-group">
				<label class="col-xs-2 text-right">游戏简介:</label>
				<div class="col-xs-4">
					<input type="text" class="input" name="gameDes" id="gameDes" value=""  placeholder="请输入游戏简介(最多20个字)">
				</div>
			</div>
			<div class="form-group">
				<label class="col-xs-2 text-right">排序编号:</label>
				<div class="col-xs-7">
					<input type="text" class="input" name="orderNum" id="orderNum" value=""  placeholder="请输入排序编号">
				</div>
			</div>
			<div class="form-group" >
				<label class="col-xs-2 text-right">是否开启:</label>
				<div class="col-xs-7">
					<td colspan="2">
	                   	<label for="port"><input id="yse" name="useFlag" type="radio" value="1"/>&nbsp;是</label>&nbsp;&nbsp;&nbsp;&nbsp;
	                   	<label for="no"><input id="no" name="useFlag" type="radio" value="0"/>&nbsp;否</label>
	               	</td>
				</div>
			</div>
			<div class="form-group" >
				<label class="col-xs-2 text-right">图片:</label>
				<div class="col-xs-10">
				<div class="cb_box cbx_img_bg">
	        		<c:if test="${not empty tlzsGame.gameIcon}">
		    			<img id="newmainImgPath" src="${pic}${tlzsGame.gameIcon}" width="160px;" height="118px;" onerror="javascript:this.src='${sysManagerV1}/images/uploadImage/noimage.gif';">
			    	</c:if>
				    <c:if test="${empty tlzsGame.gameIcon}">
				    	<img id="newmainImgPath" src="" width="160px;" height="118px;" onerror="javascript:this.src='${sysManagerV1}/images/uploadImage/noimage.gif';">
				    </c:if>
		        	<br/>
		        	<div class="mainImgPathBox">
		        		<a href="javascript:;" class="file">选择文件<input id="imgPath"  type="file" name="imgPath"  onchange="newPic(this,'mainImgPath');"/>	</a>	        		
		        	</div>
		        	<div class="exp_box" style="color:red;">建议尺寸为120*120px(jpg、bmp、png等)，并且小于500KB的图片。</div>
               	</div>
               	</div>
			</div>
			<input type="hidden" id="rowId" name="rowId" value="<%=request.getParameter("rowId")%>">
           	<input type="hidden" id="token" name="token" value="">
           	<input type="hidden" id="gameIcon" name="gameImgPage" value="${tlzsGame.gameIcon}">
           	<input type="hidden" id="flag"  value="<%=request.getParameter("flag")%>">
           	<input type="hidden" id="isReCommit" value="0">
		</sk:form>	
	</div>
</body>
<%@ include file="/view/common/footer.jsp"%>
<script type="text/javascript">
$(function(){
	var rowId = $("#rowId").val();
	if(rowId >= 0){
    	$.ajax({
            url: "${userInfoV1}/baseGame/getGameInfo",
            type: "post",
            cache: false,
            data:{"rowId":rowId},
            dataType:"json",
            async:false,
            success: function(data) {
                if (data == null) {
                    return;
                }
                $("#operatorId").val(data.operatorId);
                $("#operatorName").val(data.operatorName);
                $("#gameName").val(data.gameName);
                $("#gameDes").val(data.gameDes);
                $("#orderNum").val(data.orderNum);
                if(data.useFlag == 1){
                    $("#yse").attr("checked","true");
                    $("#no").removeAttr("checked");
                } else {
                    $("#yse").removeAttr("checked");
                    $("#no").attr("checked","true");
                }
                if(data.gameImgPath != ""){
                	$("#newmainImgPath").attr("src", "${qnPic}" + data.gameImgPath);
                } else {
                	$("#newmainImgPath").attr("src", "${sysManagerV1}/images/uploadImage/noimage.gif");
                }
                $("#gameImgPage").val(data.gameImgPage);
            }
        });
	}
	$("#token").val(sessionStorage.getItem("token"));
})

var err = {'1':'添加','2':'修改'};
var action = {'1':'/save','2':'/update'};
function add(){
    var rowId = $('#rowId').val();
    var i;
    if (!CheckUtil.isNotNull(rowId) || rowId == 0) {
        i = 1;//添加
    } else {
        i = 2;//修改
    }
    var operatorId = $.trim($("#operatorId").val());
    if (!CheckUtil.isNotNull(operatorId)) {
        layer.msg("请输入游戏运营商!");
        return;
    }
    var operatorName = $("#operatorId").find("option:selected").text();
    $("#operatorName").val(operatorName);
    
    var gameName = $.trim($("#gameName").val());
    $("#gameName").val(gameName);//渠道游戏ID去空格
    if (!CheckUtil.isNotNull(gameName)) {
        layer.msg("请输入游戏名称!");
        return;
    }
    var gameIsExist = checkGameName();
    if (gameIsExist) {
        layer.msg("游戏名称重复");
        return false;
    }
    
    var gameDes = $("#gameDes").val();
    if (!CheckUtil.isNotNull(gameDes)) {
        layer.msg("请输入游戏简介!");
        return;
    }
    if (CheckUtil.isNotNull(gameDes) && gameDes.length > 20) {
        layer.msg("游戏简介不能超过20个字!");
        return;
    }
    var orderNum = $('#orderNum').val();
    if (!CheckUtil.isNotNull(orderNum) || !CheckUtil.isInt(orderNum, "+") || orderNum == 0) {
        layer.msg("请输入正确的排序编号!");
        return;
    }
    var useFlag =$('input:radio[name="useFlag"]:checked').val();
    if (!CheckUtil.isNotNull(useFlag)) {
        layer.msg("请选择是否发布!");
        return;
    }
    var imgPath = $("#imgPath").val();//图片
    if (!CheckUtil.isNotNull(rowId) || rowId == 0) {//添加的时候
        if (!CheckUtil.isNotNull(imgPath)) {
            layer.msg("请选择所要上传的图片!");
            return;
        }
        if (CheckUtil.isNotNull(imgPath) && !CheckUtil.isImg(imgPath)) {
            layer.msg("上传文件只能为图片(jpeg、jpg、bmp三种格式)!");
            return;
        }
    } else {
        if (CheckUtil.isNotNull(imgPath)) {
            if (!CheckUtil.isImg(imgPath)) {
                layer.msg("上传文件只能为图片(jpeg、jpg、bmp、gif、png)!");
                return ;
            }
        }
    }
    //loading层
    var index = layer.load(0, {
        shade: [0.1,'#fff'] //0.1透明度的白色背景
    });
    
    $.ajax({
        url: "${userInfoV1}/baseGame" + action[i],
        type: "post",
        cache: false,
        //data:parseJson(decodeURIComponent($("#saveAndUpdateFrom").serialize())),
        data: new FormData($("#saveAndUpdateFrom")[0]),
        dataType:"json",
        processData: false,
        contentType: false,
        async:false,
        success: function(data) {
            layer.close(index);
            if(data.code == 0){
                parent.layer.msg("添加游戏成功！");
            } else if(data.code == 1){
                parent.layer.msg("修改游戏成功！");
            } else if(data.code == "-2"){
                parent.window.location.href="${sysManagerV1}/";
            } else {
                parent.layer.msg(data.message);
            }
            if(data.code >= 0){
                parent.findData();
                parent.layer.close(parent.tableIndex);
            }
        }
    });
    
/*    
    $("#saveAndUpdateFrom").attr("action", "${userInfoV1}/baseGame" + action[i]);
    if ($("#isReCommit").val() == 0) {
        $("#isReCommit").val(1);
        //loading层
        var index = layer.load(0, {
            shade: [0.1,'#fff'] //0.1透明度的白色背景
        });
        //$("#saveAndUpdateFrom").submit();
    } */
}

function checkGameName() {
    //渠道游戏ID
    var gameName = $.trim($("#gameName").val());
    $("#gameName").val(gameName);//渠道游戏ID去空格
    var flag = $("#flag").val();
    var rowId = $("#rowId").val();;
    var aa = false;
    if (CheckUtil.isNotNull(gameName)) {
        $.ajax({
            type: "post",
            cache: false,
            url: "${userInfoV1}/baseGame/checkGameIsExist",
            data:{gameName:gameName, gameId:rowId},
            dataType:"json",
            async:false,
            success: function(data) {
                aa = data;
            }
        });
    }
    return aa;  
}
</script>
</html>